﻿using MVCManage.BLL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using MVCManage.Model;
using MVCManage.Api;
using System.Xml.XPath;
using System.Security.Cryptography;
using System.Web.Http.Results;
using System.Web.UI.WebControls;
namespace MVCManage.Api.Controllers
{
    public class MenusController : ApiController
    {
        MenusBLL bll = new MenusBLL();
       
        // GET: api/Menus
        [ApiAuthorize]
        public ResultData<List<MenusDto>> Get()
        {
            //用户菜单  rid
            var auth = RequestContext.RouteData.Values["auth"] as AuthInfo;
            var rid = int.Parse(auth.Roles);

            ResultData<List<MenusDto>> result = new ResultData<List<MenusDto>>();

           AuthorityBLL bll = new AuthorityBLL();
            var auths = bll.Dal.Query().Where(a => a.roleID ==rid);

            var menus = from a in auths
                        select new MenusDto
                        {
                            MenuID = a.menuID,
                            MenuName = a.Menus.menuName,
                            Url = a.Menus.url,
                            Icon = a.Menus.icon,
                            ParentID = a.Menus.parentID,
                            LastTime = a.Menus.lastTime
                        };

            result.code = 1;
            result.msg = "ok";
            result.data = menus.ToList();
            return result;
        }

        [Route("api/Menus/Message")]
        public ResultData<List<MenusDto>> GetMenusInfo() {
           var list =from n in bll.Query()
                     select new MenusDto()
                     {
                         MenuID = n.menuID,
                         MenuName = n.menuName,
                         Url = n.url,
                         ParentID = n.parentID,
                         Icon = n.icon,
                         LastTime = n.lastTime

                     };
            ResultData<List<MenusDto>> result = new ResultData<List<MenusDto>>();
            result.code = 1;
            result.msg = "ok";
            result.data = list.ToList();
            return result;

        }
        [Route("api/Menus/Page")]
        public ResultPageList<MenusDto> GetMenusDto(string name,int page,int size) {
            int count = 0;
            var zhi = bll.GetMenus(name,page,size,out count);
            var pagelist = from n in zhi
                           select new MenusDto()
                           {
                               MenuID = n.menuID,
                               MenuName = n.menuName,
                               Url = n.url,
                               ParentID=n.parentID,
                               Icon=n.icon,
                               LastTime=n.lastTime

                           };
            ResultPageList<MenusDto> result = new ResultPageList<MenusDto>();
            result.code = 1;
            result.data = pagelist.ToList();
            result.count = count;
            return result;

        
        }

        // GET: api/Menus/5


        // POST: api/Menus
        public Result Post([FromBody] MenusDto value)
        {
            Menus menus = new Menus();
            menus.menuID = 0;
            menus.menuName = value.MenuName;
            menus.parentID = value.ParentID;
            menus.lastTime = DateTime.Now;
            menus.url = value.Url;
            menus.icon = value.Icon;
            var pd = bll.Add(menus);
            Result result = new Result();
            result.code = pd ? 1 : 0;
            result.msg = pd ? "ok" : "error";
            return result;

        }

        // PUT: api/Menus/5
        public Result Put([FromBody] MenusDto value)
        {
            var menus = bll.Query().Where(n => n.menuID == value.MenuID).FirstOrDefault();
            menus.menuName = value.MenuName;
            menus.parentID = value.ParentID;
            menus.lastTime = DateTime.Now;
            menus.url = value.Url;
            menus.icon = value.Icon;
            var pd = bll.Update(menus);
            Result result = new Result();
            result.code = pd ? 1 : 0;
            result.msg = pd ? "ok" : "error";
            return result;

        }

        // DELETE: api/Menus/5
        public Result Delete(int id)
        {
          
            var pd = bll.Delete(id);
            Result result = new Result();
            result.code = pd ? 1 : 0;
            result.msg = pd ? "ok" : "error";
            return result;
        }
        [Route("api/Menus/Deletes")]
        public Result Deletes(int[] ids) {
            var pd = bll.Deletes(ids);
            Result result = new Result();
            result.code = pd ? 1 : 0;
            result.msg = pd ? "ok" : "error";
            return result;
        }
    }
}
